<style scoped>

</style>

<template>

<div>
  
  <Modal
    v-model="IsModalShow"
    :title="modalFormTitle"
    :mask-closable="false"
    @on-cancel="cancel"
    width="600">
    <Form ref="modalForm" :model="modalForm" :label-width="100"  value=true  style="padding: 3px 60px">
      <Form-item label="bean名称" prop="beanName" :rules="{required: true, message: '必填', trigger:'blur',type:'string'}" >
        <Input v-model="modalForm.beanName" ></Input>
      </Form-item>
       <Form-item label="方法名称" prop="methodName" :rules="{required: true, message: '必填', trigger:'blur',type:'string'}" >
        <Input v-model="modalForm.methodName" ></Input>
      </Form-item>
      <Form-item label="参数" prop="params" :rules="{required: true, message: '必填', trigger:'blur',type:'string'}" >
        <Input v-model="modalForm.params" ></Input>
      </Form-item>
      <Form-item label="cron表达式" prop="cronExpression" :rules="{required: true, message: '必填', trigger:'blur',type:'string'}">
        <Input v-model="modalForm.cronExpression" ></Input>
      </Form-item>
      <Form-item label="备注" >
        <Input v-model="modalForm.remark" ></Input>
      </Form-item>
    </Form>
    <div slot="footer">
      <Button type="default"  @click="cancel" >关闭</Button>
      <Button type="primary"  :loading="modalForm_loading" @click="saveForm('modalForm')">保存</Button>
    </div>
  </Modal>
</div>

</template>

<script>
import {updateSchedule,saveSchedule} from '@/api/system'
export default {
    props:{
      parentForm: {
        type: Object,
        default: function () {
          return {

          }
        }
      },
      modalShow:{
        type: Boolean,
        default: true,
      },
      modalFormTitle:{
        type: String,
        default: '新增定时任务',
      }
    },
    data() {
        return {
          IsModalShow:false,
          modalForm:{
          },
          modalForm_loading:false
        }
    },
    watch:{
      modalShow(curVal,oldVal){
        if(curVal){
          this.modalForm = JSON.parse(JSON.stringify(this.parentForm))
        }
        this.IsModalShow = curVal;       
      }
    },
    created(){

    },
    mounted(){
    },
    methods: {
      cancel() {
          this.$emit('listenModalForm');
      },
      saveForm(name) {
        this.$refs[name].validate( async (valid) => {
          if (valid) {
            this.modalForm_loading=true;
            let params = JSON.parse(JSON.stringify(this.modalForm));

            try{
              let result;
              if(params.jobId){
                result = await updateSchedule(params);
              }else{
                result = await saveSchedule(params);
              }
              
              if (result.code=="0") {
                this.$Message.success('提交成功!');
                this.$emit('listenModalForm');
                this.$emit('refreshTableList');
              }else{
                this.$Message.error(result.msg);
              }
            }catch(e){
              this.$Message.error('服务器异常，稍后再试');
            }
            this.modalForm_loading=false;
          } else {
            this.$Message.error('表单验证失败!');
          }
        })
      },
    }
}

</script>
